#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys,os,socket,time,threading
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import QDeclarativeView
from QtMobility import Sensors
from PySide import QtOpenGL

class Console(QObject):
	@Slot(str)
	def savesettings(self,value):
		print "Settings: ",value
		f=open('/home/user/MyDocs/Groundwork/settings.txt','w')
		f.write(value+'\n')
		f.close()
	@Slot()
	def kill(self):
		os.system("ps | grep 'ffmpeg -f x11grab -y -s 854x480' | awk '{print $1}'")
		os.system("ps | grep 'ffmpeg -f x11grab -y -s 854x480' | awk '{print $1}' | xargs kill")
	def begin(self):
		x,y,z = self.runner()
		join = "echo '"+str(x)+"' | nc -q 1 "+str(y)+" 9001"
		thread1 = threading.Thread(target=self.do,args=[(join)])
		thread2 = threading.Thread(target=self.do,args=[(z)])
		
		thread1.start()
		thread2.start()
		print x,y,z
	def runner(self):
		
		c = os.system("test -e /home/user/MyDocs/Groundwork/settings.txt")
		if c !=0:
			os.system("mkdir /home/user/MyDocs/Groundwork")
			Drecord='0'
			Precord='0'
			Player='0'
			Raw='0'
			fps='5'
			command="while true; do ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi pipe: | nc -l -p 9001;done"
			setup="mplayer -"
		else:
			r=open('/home/user/MyDocs/Groundwork/settings.txt','r').readlines()
			print r
			config=r[0].strip()
			fps=r[1].strip()
			ip=r[2].strip()
			print "Config:",config,"Frames/s:",fps,"I.P. Address: ",ip
			if config=="0000":
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi pipe: | nc -l -p 9001"
				setup="mplayer -"
			if config=="1000":
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi pipe: | nc -l -p 9001"
				setup="mplayer -dumpstream -;mv stream.dump n950screen.avi"
			if config=='0001':
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - | nc -l -p 9001;done"
				setup="mplayer -demuxer rawvideo -rawvideo w=854:h=480:format=bgr16 -fixed-vo -"
			if config=='0010':
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi pipe: | nc -l -p 9001"
				setup='vlc -'
			if config=="0011":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - | nc -l -p 9001;done"
				setup="while true; do nc -l 9000;done | zcat | ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr32 -s 480x854 -i - -f mpeg2video - | vlc -"
			if config=="0100":
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi screen.avi& cat screen.avi | nc -l -p 9001"
				setup="mplayer -"
			if config=="0101":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - > n950screen.raw && cat n950screen.raw | nc -l -p 9001;done"
				setup="mplayer-demuxer rawvideo -rawvideo w=854:h=480:format=bgr16 -fixed-vo"
			if config=='0110':
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi screen.avi& cat screen.avi | nc -l -p 9001"
				setup="vlc -"
			if config=="0111":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - > n950screen.raw && cat n950screen.raw | nc -l -p 9001;done"
				setup="ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr16 -s 480x854 -i - -f mpeg2video - | vlc -"
			if config=="1001":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - | nc -l -p 9001;done"
				setup="mplayer -demuxer rawvideo -rawvideo w=854:h=480:format=bgr16 -fixed-vo -dumpstream -;mv stream.dump n950screen.avi"
			if config=="1010":
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi pipe: | nc -l -p 9001"
				setup="vlc - :demux=dump :demuxdump-file=n950screen.avi& vlc --repeat n950screen.avi"
			if config=="1011":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - > n950screen.raw && cat n950screen.raw | nc -l -p 9001;done"
				setup="ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr16 -s 480x854 -i - -f mpeg2video - | vlc -"
			if config=="1100":
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi screen.avi& cat screen.avi | nc -l -p 9001"
				setup="mplayer -dumpstream -;mv stream.dump n950screen.avi"
			if config=="1101":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - > n950screen.raw && cat n950screen.raw | nc -l -p 9001;done"
				setup="mplayer -demuxer rawvideo -rawvideo w=854:h=480:format=bgr16 -fixed-vo -dumpstream -;mv stream.dump n950screen.avi"
			if config=="1110":
				command="ffmpeg -f x11grab -y -s 854x480 -r "+fps+" -i :0.0 -f avi screen.avi& cat screen.avi | nc -l -p 9001"
				setup="vlc - :demux=dump :demuxdump-file=n950screen.avi& vlc --repeat n950screen.avi"
			if config=="1111":
				command="ffmpeg -f x11grab -y -s 854x480 -r 4 -i :0.0 -vcodec copy -f rawvideo - > n950screen.raw && cat n950screen.raw | nc -l -p 9001;done"
				setup="ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt bgr16 -s 480x854 -i - -f mpeg2video - | vlc - :demux=dump :demuxdump-file=n950screen.mpg& vlc --repeat n950screen.mpg"
		return setup,ip,command
	def do(self,arb):
		os.system(arb)
# Create Qt application and the QDeclarative view & accelerometer
app = QApplication(sys.argv)
view = QDeclarativeView()
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)

# Create an URL to the QML file
url = QUrl('/usr/share/groundwork/qml/main.qml')
# Set the QML file and show
view.setSource(url)
#define button and signal respnse
root = view.rootObject()
button=root.findChild(QObject,"rootWindow")
con=Console()
root.buttonClicked.connect(lambda: con.begin())
context=view.rootContext()
context.setContextProperty("con",con)
#button.clicked.connect(lambda: start())

view.showFullScreen()
# Enter Qt main loop
sys.exit(app.exec_())
#help
